package com.atguigui.leetcode1;

/**
 * 1605.给定行和列的和求可行矩阵
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/3/14 8:50
 */
public class P1605FindValidMatrixGivenRowAndColumnSums {
    public static void main(String[] args) {
        Solution solution = new P1605FindValidMatrixGivenRowAndColumnSums().new Solution();
        // TO TEST
    }

    class Solution {
        public int[][] restoreMatrix(int[] rowSum, int[] colSum) {
            int n = rowSum.length, m = colSum.length;
            int[][] matrix = new int[n][m];
            int i = 0, j = 0;
            while (i < n && j < m) {
                int v = Math.min(rowSum[i], colSum[j]);
                matrix[i][j] = v;
                rowSum[i] -= v;
                colSum[j] -= v;
                if (rowSum[i] == 0) {
                    ++i;
                }
                if (colSum[j] == 0) {
                    ++j;
                }
            }

            return matrix;
        }
    }
}
